// client.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include "MqttClient.h"

int main()
{
    std::cout << "MqttClient Demo.." << std::endl;


	MqttClient client = MqttClient("123","tcp://iot.eclipse.org:1883", "456" );
	bool success = client.init();
	if (success) 
	{
		std::cout << "init success, remote : tcp://iot.eclipse.org:1883" << std::endl;
		char * test_topic = (char *)"test";

		client.subscribe(test_topic);

		MQTTClient_message msg = {};
		char * content = (char *)"test message";
		bool success = client.upload(content, 0, test_topic, &msg);
		if (success) 
		{
			std::cout << "upload success:"<< content << std::endl;
		}
		else
		{
			std::cout << "upload fail:"<< content << std::endl;
		}
		
		char * t = {};

		MqttClient_Messsage * p = client.read(5000);

		if (p != NULL)
		{
			std::cout << "receive success: " << p->topic_name << ", length :"<< p->payloadlen << ", "<<(char *)(p->payload) << std::endl;
		}
		else
		{
			std::cout << "receive fail" << std::endl;
		}
	}
	else 
	{
		std::cout << "init fail" << std::endl;
	}

	char ch;
	std::cin >> ch;
}

